package com.lanhe.member.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.member.domain.LhStatWeeklyIncome;
import com.lanhe.member.domain.LhStatYearIncome;
import com.lanhe.member.mapper.LhStatWeeklyIncomeMapper;
import com.lanhe.member.service.ILhStatWeeklyIncomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * (LhStatWeeklyIncome)表服务实现类
 *
 * @author zhanggl
 * @since 2023-12-11 09:58:47
 */
@Service
public class LhStatWeeklyIncomeServiceImpl extends ServiceImpl<LhStatWeeklyIncomeMapper, LhStatWeeklyIncome> implements ILhStatWeeklyIncomeService {

	@Autowired
	private LhStatWeeklyIncomeMapper lhStatWeeklyIncomeMapper;

	/**
	 * 新增
	 *
	 * @param statWeeklyIncome 实体
	 * @return int
	 * @author zhanggl
	 * @date 2023/12/11
	 */
	@Override
	public int insertStatWeeklyIncome(LhStatWeeklyIncome statWeeklyIncome) {

		LambdaQueryWrapper<LhStatWeeklyIncome> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(LhStatWeeklyIncome::getMemberId, statWeeklyIncome.getMemberId());
		lambdaQueryWrapper.eq(LhStatWeeklyIncome::getYear, statWeeklyIncome.getYear());
		lambdaQueryWrapper.eq(LhStatWeeklyIncome::getMonthly, statWeeklyIncome.getMonthly());
		lambdaQueryWrapper.eq(LhStatWeeklyIncome::getWeekly, statWeeklyIncome.getWeekly());
		LhStatWeeklyIncome statWeekly = lhStatWeeklyIncomeMapper.selectOne(lambdaQueryWrapper);
		if (ObjectUtil.isNotNull(statWeekly)) {

			statWeeklyIncome.setUpdateTime(new Date());
			statWeeklyIncome.setAmount(statWeekly.getAmount().add(statWeeklyIncome.getAmount()));
			return lhStatWeeklyIncomeMapper.update(statWeeklyIncome,lambdaQueryWrapper);
		} else {

			statWeeklyIncome.setCreateTime(new Date());
			return lhStatWeeklyIncomeMapper.insert(statWeeklyIncome);
		}
	}

	@Override
	public List<LhStatWeeklyIncome> rankingList(Integer pageNum, Integer pageSize) {

		// 获取当前日期
		LocalDate currentDate = LocalDate.now();
		WeekFields weekFields = WeekFields.of(Locale.getDefault());
		int currentWeek = currentDate.get(weekFields.weekOfWeekBasedYear());

		LambdaQueryWrapper<LhStatWeeklyIncome> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(LhStatWeeklyIncome::getYear,currentDate.getYear());
		lambdaQueryWrapper.eq(LhStatWeeklyIncome::getMonthly,currentDate.getYear()+""+currentDate.getMonthValue());
		lambdaQueryWrapper.eq(LhStatWeeklyIncome::getWeekly,currentWeek);
		lambdaQueryWrapper.orderByDesc(LhStatWeeklyIncome::getAmount);
		return lhStatWeeklyIncomeMapper.selectList(lambdaQueryWrapper);
	}

	@Override
	public LhStatWeeklyIncome getInfo(LhStatWeeklyIncome statWeeklyIncome) {
		LambdaQueryWrapper<LhStatWeeklyIncome> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(LhStatWeeklyIncome::getMemberId, statWeeklyIncome.getMemberId());
		lambdaQueryWrapper.eq(LhStatWeeklyIncome::getYear, statWeeklyIncome.getYear());
		lambdaQueryWrapper.eq(LhStatWeeklyIncome::getMonthly, statWeeklyIncome.getMonthly());
		lambdaQueryWrapper.eq(LhStatWeeklyIncome::getWeekly, statWeeklyIncome.getWeekly());
		return lhStatWeeklyIncomeMapper.selectOne(lambdaQueryWrapper);
	}
}

